﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Dispatcher.Extensions;
using Dispatcher.Views;

namespace Dispatcher.Helpers
{
    /// <summary>
    /// Tree View Drag Drop Helper.
    /// </summary>
    internal class TreeViewDragDropHelper
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TreeViewDragDropHelper"/> class.
        /// </summary>
        /// <param name="commonTreeView">The common tree view.</param>
        public TreeViewDragDropHelper(CommonTreeView commonTreeView)
        {
            TreeView = commonTreeView;
        }

        /// <summary>
        /// Gets or sets the tree view.
        /// </summary>
        /// <value>
        /// The tree view.
        /// </value>
        private CommonTreeView TreeView { get; set; }

        /// <summary>
        /// Drags this instance.
        /// </summary>
        public void Drag()
        {
            var selectedItems =
                TreeView.Tree.GetVisualChildren<TreeViewItem>().Where(i => i.IsSelected).Select(i => i.DataContext).ToList();
            var container = new DragDropContainer
            {
                ItemType = TreeView.Tree.DataContext.GetType().GetGenericArguments()[0],
                Items = selectedItems,
            };

            DragDrop.DoDragDrop(TreeView, container, DragDropEffects.Copy);
        }
    }
}
